package com.woniuxy.consumer;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.woniuxy.entity.Doctor;
import com.woniuxy.entity.Schedule;
import com.woniuxy.mapper.DoctorMapper;
import com.woniuxy.service.ScheduleService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Description:加排班表
 * @Author: NoNo
 * @Date: 2024/4/20 11:00
 */
@Component
public class AddscheduleConsumer {

    @Autowired
    DoctorMapper doctorMapper;

    @RabbitListener(queues = "addScheduleQueue")
    public void receive(String msg, Channel channel, Message message) {
        Doctor doctor = JSON.parseObject(msg, Doctor.class);
        if(!doctor.getScDate().equals("")){
            Schedule schedule = doctorMapper.queryByScDateAndDocId(doctor);
            if (schedule == null) {
                doctorMapper.addSchedule(doctor);
            }
        }


        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
